<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-form :inline="true" :model="searchMap" class="demo-form-inline">
      <el-form-item>
        <el-input
          v-model="searchMap.data.name"
          placeholder="景区关键字"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-select v-model="searchMap.data.attractionsId" placeholder="请选择景区">
          <el-option label="请选择景区" value=""></el-option>
          <el-option
            v-for="item in scenicList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="searchData()">搜索</el-button>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          @click="
            dialogFormVisible = true;
            scenicDetail = {};imgsArr=[];
          "
          >添加景区详情</el-button
        >
      </el-form-item>
      <el-form-item>
        <el-button type="danger" @click="delAllIds()">批量删除</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格 -->
    <el-table
      ref="multipleTable"
      :data="scenicDetailList"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection"> </el-table-column>
      <el-table-column prop="name" label="景区名称" width="150"> </el-table-column>
      <el-table-column prop="imgs" label="景区图片" width="200"> 
        <template slot-scope="scope">
         <div><p v-for="(img,index) in scope.row.imgs" :key="index"><img :src="img" style="width:50px; height:80px" /></p></div>
        </template>
      </el-table-column>
      <el-table-column prop="trait" label="景区特色" width="360"> </el-table-column>
      <el-table-column prop="site" label="景区位置" width="180"> </el-table-column>
      <el-table-column prop="businessHours" label="营业时间" width="150"> </el-table-column>
      
      <el-table-column label="操作" width="150">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="block" style="float: right">
      <span class="demonstration"></span>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="searchMap.page"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="5"
        layout="total, sizes, prev, pager, next, jumper"
        :total="this.total"
      >
      </el-pagination>
    </div>
    <!-- 添加对话框 -->
    <el-dialog title="景区详情" :visible.sync="dialogFormVisible">
      <el-form :model="scenicDetail">
        <el-form-item label="选择景区">
          <el-select v-model="scenicDetail.attractionsId" placeholder="请选择景区">
            <el-option
              v-for="item in scenicList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="景区名称">
          <el-input v-model="scenicDetail.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="景区图片">
          <el-upload
            :headers=this.headers
            ref='upload'
            action="http://192.168.53.83:8040/upload"
            list-type="picture-card"
            name="headerImage"
            :on-success="uploadSuccess"
          >
            <!-- <img  :src="this.scenicDetail.imgs"/> -->
            <i class="el-icon-plus"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="景区特色">
          <el-input v-model="scenicDetail.trait" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="景区位置">
          <el-input v-model="scenicDetail.site" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="营业时间">
          <el-input v-model="scenicDetail.businessHours" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="add()">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 修改对话框 -->
    <el-dialog title="景区详情" :visible.sync="dialogFormVisible_update">
      <el-form :model="scenicDetail">
        <el-form-item label="选择景区">
          <el-select v-model="scenicDetail.attractionsId" placeholder="请选择景区" aria-readonly="readonly">
            <el-option
              v-for="item in scenicList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="景区名称">
          <el-input v-model="scenicDetail.name" autocomplete="off"  aria-readonly="readonly"></el-input>
        </el-form-item>
        <el-form-item label="景区图片">
          <el-upload
            :headers=this.headers
            ref='upload'
            action="http://192.168.53.83:8040/upload"
            list-type="picture-card"
            name="headerImage"
            :on-success="uploadSuccess"
          >
            <img  v-for="(img ,index) in scenicDetail.imgs" :key="index"   :src="img" style="width:30px; height:50px"/>
            <i class="el-icon-plus"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="景区特色">
          <el-input v-model="scenicDetail.trait" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="景区位置">
          <el-input v-model="scenicDetail.site" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="营业时间">
          <el-input v-model="scenicDetail.businessHours" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible_update = false">取 消</el-button>
        <el-button type="primary" @click="update()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  search,
  scenicSearch,
  addScenicDetail,
  del,
  delAll,
} from "@/api/scenicDetail";
export default {
  data() {
    return {
      searchMap: {
        data: {},
        page: 1,
        limit: 5,
      },
      headers:{},
      scenicDetailList: [], //景区详情list
      scenicList: [], //景区list
      total: 0,
      dialogFormVisible: false,
      dialogFormVisible_update:false,
      scenicDetail:{}, //景区详情entity
      delScenicDetail: {}, //删除scenicDetail
      ids:[],//批量删除id list
      imgsArr:[]
    };
  },
  methods: {
    
    //文件上传
    uploadSuccess(response, file, fileList) {
      this.imgsArr.push(response.data)

      this.scenicDetail.imgs=this.imgsArr
      console.log(this.scenicDetail.imgs)
    },
    //搜索
    searchData() {
      search(this.searchMap).then((res) => {

        if (res.success) {
          this.scenicDetailList = res.data;
          this.scenicDetailList.forEach(item=>{
            item.imgs=item.imgs.split(",");
            
          })
          this.total = res.total;
        }
      });
    },
    handleSizeChange(size) {
      this.searchMap.limit = size;
      this.searchData();
    },
    handleCurrentChange(currentPage) {
      this.searchMap.page = currentPage;
      this.searchData();
    },
    //添加
    add(){
       this.headers={'token':localStorage.getItem('token')}
       this.scenicDetail.imgs=this.scenicDetail.imgs.join(",");
      addScenicDetail(this.scenicDetail).then((res) => {
        if (res.success) {
          this.$message({
            message: res.msg,
            type: "success",
          });
          this.searchData();
        } else {
          this.$message({
            message: res.msg,
            type: "error",
          });
        }
        this.dialogFormVisible = false;
      });
    },
    //编辑
    handleEdit(index, row) {
      this.dialogFormVisible_update = true;
     
      this.scenicDetail = row;
      this.$nextTick(()=>{
               this.$refs.Upload.clearFiles();//清空图片
             });
    },
    update(){
       this.scenicDetail.imgs=this.scenicDetail.imgs.join(",");
      addScenicDetail(this.scenicDetail).then((res) => {
        if (res.success) {
          this.$message({
            message: res.msg,
            type: "success",
          });
          this.searchData();
        } else {
          this.$message({
            message: res.msg,
            type: "error",
          });
        }
        this.dialogFormVisible_update = false;
      });
    },
    //删除
    handleDelete(index, row) {
      this.delScenicDetail = row;
      if(this.delScenicDetail.imgs[1]!=''){
        this.delScenicDetail.imgs=this.delScenicDetail.imgs.join(",");
      }
      
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        del(this.delScenicDetail).then((res) => {
          if (res.success) {
            this.searchData();
            this.$message({
              showClose: true,
              message: res.msg,
              type: "success",
            });
          } else {
            this.$message({
              showClose: true,
              message: res.msg,
              type: "error",
            });
          }
        });
      });
    },
    //批量删除
    handleSelectionChange(val) {
      this.ids=[]
      console.log(val)
      val.forEach(element => {

        this.ids.push(element.id)
        console.log(element.id)
      });
      console.log(this.ids)
    },
    delAllIds(){
       this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        delAll(this.ids).then(res=>{
          if (res.success) {
              this.searchData();
              this.$message({
                showClose: true,
                message: res.msg,
                type: "success",
              });
            } else {
              this.$message({
                showClose: true,
                message: res.msg,
                type: "error",
              });
            }
        })
      });
      
    }
  },
  created() {
    this.searchData();
    scenicSearch().then((res) => {
      if (res.success) {
        this.scenicList = res.data;
      }
    });
  },
};
</script>